package com.hutao.manage.util;
import java.io.BufferedReader;  
import java.io.ByteArrayOutputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.PrintWriter;  
import java.util.ArrayList;  
import java.util.List;  
import java.util.zip.ZipEntry;  
import java.util.zip.ZipOutputStream;



public class IOUtils {

	 /** 
     * 将输入流转换成字节流 
     * @param input 
     * @return 
     * @throws Exception 
     */  
    public static byte[] toBytes(InputStream input) throws Exception {  
        byte[] data = null;  
        try {  
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();  
            byte[] b = new byte[1024];  
            int read = 0;  
            while ((read = input.read(b)) > 0) {  
                byteOut.write(b, 0, read);  
            }  
            data = byteOut.toByteArray();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            input.close();  
        }  
        return data;  
    }  
  
    /** 
     * 将文件读取为一个字符串 
     *  
     * @param input 
     * @return 
     * @throws Exception 
     */  
    public static String toString(File file) throws Exception {  
        return toString(new FileInputStream(file));  
    }  
  
    /** 
     * 将输入流转换为一个串 
     *  
     * @param input 
     * @return 
     * @throws Exception 
     */  
    public static String toString(InputStream input) throws Exception {  
        return toStringWithLineBreak(input, null);  
    }  
  
    /** 
     * 以指定编码格式将输入流按行置入一个List<String> 
     *  
     * @param input 
     * @return 
     * @throws Exception 
     */  
    public static List<String> toLines(InputStream input, String encoding)  
            throws Exception {  
        InputStreamReader insreader = new InputStreamReader(input, encoding);  
        BufferedReader bin = new BufferedReader(insreader);  
        List<String> lines = new ArrayList<String>();  
        String line;  
        while ((line = bin.readLine()) != null) {  
            lines.add(line);  
        }  
        bin.close();  
        insreader.close();  
        return lines;  
    }  
  
    /** 
     * 以GBK格式将输入流按行置入一个List<String> 
     *  
     * @param input 
     * @return 
     * @throws Exception 
     */  
    public static List<String> toLines(InputStream input) throws Exception {  
        return toLines(input, "GBK");  
    }  
  
    /** 
     * 转换为每行补充指定换行符(例如："/n"，"</br>") 
     *  
     * @param input 
     * @param lineBreak 
     * @return 
     * @throws Exception 
     */  
    public static String toStringWithLineBreak(InputStream input,  
            String lineBreak) throws Exception {  
        List<String> lines = toLines(input);  
        StringBuilder sb = new StringBuilder(20480);  
        for (String line : lines) {  
            sb.append(line);  
            if (lineBreak != null) {  
                sb.append(lineBreak);  
            }  
        }  
        return sb.toString();  
    }  
  
    /** 
     * 将字符串转出到指定文件 
     * @param saveFile 
     * @param content 
     */  
    public static void toFile(File saveFile, String content) {  
        File parent = saveFile.getParentFile();  
        if (!parent.exists()) {  
            parent.mkdirs();  
        }  
        PrintWriter out = null;  
        try {  
            out = new PrintWriter(new FileWriter(saveFile));  
            out.print(content);  
            out.flush();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (out != null) {  
                out.close();  
            }  
        }  
    }  
  
    /** 
     * 将一组文件打zip包 
     *  
     * @param srcFiles 
     * @param targetFileName 
     * @throws IOException 
     */  
    public static void filesToZip(List<File> srcFiles, String targetFileName)  
            throws IOException {  
        String fileOutName = targetFileName + ".zip";  
        byte[] buf = new byte[1024];  
        FileInputStream in = null;  
        FileOutputStream fos = null;  
        ZipOutputStream out = null;  
        try {  
            fos = new FileOutputStream(fileOutName);  
            out = new ZipOutputStream(fos);  
            for (File file : srcFiles) {  
                in = new FileInputStream(file);  
                out.putNextEntry(new ZipEntry(file.getName()));  
                int len;  
                while ((len = in.read(buf)) != -1) {  
                    out.write(buf, 0, len);  
                }  
                if (in != null) {  
                    in.close();  
                }  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (in != null) {  
                in.close();  
            }  
            if (fos != null) {  
                out.closeEntry();  
                out.close();  
                fos.close();  
            }  
        }  
    }  
  
    public static String readFile(File file) {
		FileInputStream fis = null;
		BufferedReader br = null;
		InputStreamReader isr = null;
		StringBuffer sb = new StringBuffer();
		
		try {
		    fis = new FileInputStream(file); 
		    //部署到服务器上有可能出现中文乱码问题
		    isr = new InputStreamReader(fis, "gbk");
			br = new BufferedReader(isr); 
			int  length = 0;
			char [] ch = new char[1024];
			while ((length = br.read(ch)) != -1) {
				sb.append(new String(ch, 0, length));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if (isr != null) {
				try {
					isr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return sb.toString();
	}
    
//    public static void main(String[] args) {  
//        try {  
////        	String filePath = "D:\\publics\\progressNote\\html\\4244bd991e8e44928930394829e7ec7d\\D295750_34763-12_20171126185310_1.html";
////        	filePath = "C:\\Users\\h2oco2\\Desktop\\新建文本文档.txt";
////            File doc1 = new File(filePath);  
////            String htmlstr =  IOUtils.readFile(doc1);  
////            System.out.println(htmlstr);
////            HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
////            imageGenerator.loadHtml(htmlstr);
////
////            imageGenerator.getBufferedImage();
////
////            imageGenerator.saveAsImage("D:\\publics\\progressNote\\html\\4244bd991e8e44928930394829e7ec7d\\D295750_34763-12_20171126185310_1.png");
//        	
//            ImageRenderer render = new ImageRenderer();
//            String url = "http://localhost:8090/progressNote/html/06ce108d6ecc470a89c6cff36fa4212b/D295751_34763-12_20171126185328_1.html";
//            url = "http://cssbox.sourceforge.net";
//            FileOutputStream out = new FileOutputStream(new File("C:\\Users\\h2oco2\\Desktop\\aa.png"));
//        	render.renderURL(url, out, ImageRenderer.Type.PNG);
//        	System.out.println("end");
//        	
//        } catch (Exception e) {  
//            e.printStackTrace();  
//        }  
//    }  
	
	
	

}
